home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / B-C / C++ FAQ Reference 1.0 / C++ FAQ Reference 1.0.rsrc / TEXT_428.txt < prev    next >
Encoding:
Text File  |  1993-06-30  |  668 b   |  25 lines

  1. Operator overloading allows the basic C/C++ operators to have user-defined meanings on user-defined types (classes).  They are syntactic sugar for equivalent function calls; ex:
  2.  
  3.     class X {
  4.       //...
  5.     public:
  6.       //...
  7.     };
  8.  
  9.     X add(X, X);    //a top-level function that adds two X's
  10.     X mul(X, X);    //a top-level function that multiplies two X's
  11.  
  12.     X f(X a, X b, X c)
  13.     {
  14.       return add(add(mul(a,b), mul(b,c)), mul(c,a));
  15.     }
  16.  
  17. Now merely replace 'add' with 'operator+' and 'mul' with 'operator*':
  18.  
  19.     X operator+(X, X);    //a top-level function that adds two X's
  20.     X operator*(X, X);    //a top-level function that multiplies two X's
  21.  
  22.     X f(X a, X b, X c)
  23.     {
  24.       return a*b + b*c + c*a;
  25.     }